import sys
sys.path.append('/Users/chengyanan/PycharmProjects/kuai8_appium')
from uitls.port import Port
from uitls.dos_cmd import DosCmd
import threading
import multiprocessing
from uitls.write_user_command import WriteUserCommand
import time
class Server:
    def __init__(self):
        self.dos = DosCmd()
        self.device_list = self.get_devices()
        self.write_file=WriteUserCommand()


    def get_devices(self):
        # 获取设备信息
        devices_list = []
        result_list = self.dos.excute_cmd_result('adb devices')
        if len(result_list) >= 2:
            for i in result_list:
                if 'List ' in i:
                    continue
                devices_info = i.split('\t')
                if devices_info[1] == 'device':
                    devices_list.append(devices_info[0])
            return devices_list
        else:
            print('adb deives没有拿到设备信息')
            return None

    def create_port_list(self, start_port):
        '''
        创建可用端口
        :return:
        '''
        port = Port()
        port_list = []
        port_list = port.create_port_list(start_port, self.device_list)
        return port_list

    def create_command_list(self,i):
        # appium -p 4700 -bp 4701 -U 127.0.0.1:21503
        command_list = []
        appium_port_list = self.create_port_list(4700)
        bootstarp_port_list = self.create_port_list(4900)
        device_list = self.device_list
        command = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str(bootstarp_port_list[i]) + ' -U ' + \
                    device_list[i] + ' --no-reset --session-override  --log ../report/Appium_'+device_list[i]+'_.log'
        command_list.append(command)
        self.write_file.write_data(i=i,device=device_list[i],bp=str(bootstarp_port_list[i]),port=str(appium_port_list[i]))
        return command_list

    def start_server(self, i):
        self.start_list = self.create_command_list(i)
        print('start_server========',i,self.start_list[0])
        time.sleep(10)
        self.dos.excute_cmd(self.start_list[0])

    def kill_server(self):
        Server_list = self.dos.excute_cmd_result('lsof -i:netxms-agent')
        print("Kill server:", Server_list)
        if len(Server_list) > 0:
            self.dos.excute_cmd('killall node')

    def main(self):
        thread_list=[]
        self.kill_server()
        self.write_file.clear_data()
        for i in range(len(self.device_list)):
            # appium_start = threading.Thread(target=self.start_server, args=(i,))
            appium_start=multiprocessing.Process(target=self.start_server, args=(i,))
            thread_list.append(appium_start)
        for j in thread_list:
            print(j)
            j.start()
        time.sleep(25)

if __name__ == '__main__':
    server = Server()
    # print(server.get_devices())#获取设备信息['127.0.0.1:62001']
    # print(server.create_command_list())#['appium -p 4700 -bp 4900 -U 127.0.0.1:62001 --no-reset --session-override']
    print(server.main())
